package com.coolw.design.pattern.structure.proxy.jdkdynamicproxy;

import java.lang.reflect.Proxy;

/**
 * （买票）JDK代理工厂
 *
 * @author coolw
 * @date 2019-12-27 08:57
 */
public class JdkProxyFactory {

    /**
     * 目标对象
     */
    private final Object target;

    public JdkProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    System.out.println("代理买票开始");
                    //执行目标对象方法
                    Object returnValue = method.invoke(target, args);
                    System.out.println("代理买票成功");
                    return returnValue;
                }
        );
    }
}
